package com.ruoyi.web.controller.byhealth;


import com.alibaba.fastjson.JSON;
import com.ruoyi.by.health.domain.entity.ByhealthEmployee;
import com.ruoyi.by.health.domain.form.SystemConfigParam;
import com.ruoyi.by.health.domain.vo.ByhealthEmpVoteLogVo;
import com.ruoyi.by.health.service.IByhealthEmpVotesService;
import com.ruoyi.by.health.service.IByhealthEmployeeService;
import com.ruoyi.by.health.service.IByhealthPlaybillService;
import com.ruoyi.by.health.service.IEmployeeMessageWallService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 员工信息管理
 *
 * @author zhangjq
 */
@Slf4j
@Api(value = "员工信息管理", tags = "员工信息管理")
@RestController
@RequestMapping("/byhealth/employee")
public class ByhealthEmployeeController extends BaseController {
    @Resource
    RedisTemplate<String, Object> redisTemplate;
    @Autowired
    public IByhealthEmployeeService employeeService;
    @Autowired
    public IEmployeeMessageWallService messageService;
    @Autowired
    public IByhealthPlaybillService playbillService;
    @Autowired
    public IByhealthEmpVotesService votesService;

    /**
     * 获取用户列表
     */
    @ApiOperation("获取员工列表")
    //@PreAuthorize("@ss.hasPermi('byhealth:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(ByhealthEmployee emp) {
        startPage();
        List<ByhealthEmployee> list = employeeService.selectEmployeeList(emp);
        return getDataTable(list);
    }

    /**
     * 获取小程序配置信息
     *
     * @return 小程序配置信息
     */
    @ApiOperation("获取小程序配置信息")
    @GetMapping("getConfigList")
    public AjaxResult getConfigList() {

        Map<Object, Object> entries = redisTemplate.opsForHash().entries("byhealth_system_config");
        return AjaxResult.success(entries);
    }

    /**
     * 更新小程序配置信息
     *
     * @return 更新小程序配置信息
     */
    @ApiOperation("更新小程序配置信息")
    @PostMapping("updateConfig")
    public AjaxResult updateConfig(@RequestBody SystemConfigParam configParam) {

        log.info("updateConfig:" + configParam.toString());
        redisTemplate.opsForHash().put("byhealth_system_config", configParam.getKey(), configParam.getValue());
        Map<Object, Object> entries = redisTemplate.opsForHash().entries("byhealth_system_config");
        log.info("updateConfig.entries:{}:" + JSON.toJSONString(entries));
        return AjaxResult.success();
    }

    @ApiOperation("员工信息导入")
    @Log(title = "员工管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('byhealth:employee:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ByhealthEmployee> util = new ExcelUtil<ByhealthEmployee>(ByhealthEmployee.class);
        List<ByhealthEmployee> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = employeeService.importEmployee(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ByhealthEmployee> util = new ExcelUtil<ByhealthEmployee>(ByhealthEmployee.class);
        util.importTemplateExcel(response, "用户数据");
    }


    @ApiOperation("投票结果")
    @GetMapping("/statsVote")
    public AjaxResult statsVote() {
        return votesService.selectPlaybillList();
    }

    /**
     * 获取员工能投票结果列表
     */
    @ApiOperation("获取员工能投票结果列表")
    //@PreAuthorize("@ss.hasPermi('byhealth:employee:list')")
    @GetMapping("/listVoteLog")
    public TableDataInfo listVoteLog(ByhealthEmpVoteLogVo voteLogVo) {
        startPage();
        List<ByhealthEmpVoteLogVo> list = votesService.getEmpVoteLogList(voteLogVo);
        return getDataTable(list);
    }
}
